package com.example.firstjavamqtt.service.log.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.firstjavamqtt.model.base.BaseDeviceLogDto;
import com.example.firstjavamqtt.model.log.TemperatureHumidityMonitorLog;
import com.example.firstjavamqtt.service.log.InfraredLogService;
import com.example.firstjavamqtt.common.excepiton.GlobalException;
import com.example.firstjavamqtt.common.result.ResultCodeEnum;
import com.example.firstjavamqtt.mapper.InfraredLogMapper;
import com.example.firstjavamqtt.model.device.InfraredMonitor;
import com.example.firstjavamqtt.model.log.InfraredMonitorLog;
import com.example.firstjavamqtt.utils.BeanListUtils;
import com.example.firstjavamqtt.utils.LogRequestUtil;
import com.example.firstjavamqtt.utils.RedisUtil;
import com.example.firstjavamqtt.utils.SqlUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InfraredLogServiceImpl extends ServiceImpl<InfraredLogMapper, InfraredMonitorLog> implements InfraredLogService {

    @Autowired
    RedisUtil redisUtil;

    //保存红外日志
    @Override
    public void saveInfraredMonitorLog(String string, HttpServletRequest request) {
        InfraredMonitor infraredMonitor = JSONObject.parseObject(string, InfraredMonitor.class);
        if(!infraredMonitor.isOnline()){
            throw new GlobalException(ResultCodeEnum.OFFLINE);
        }
        InfraredMonitorLog infraredMonitorLog = new InfraredMonitorLog();
        BeanUtils.copyProperties(infraredMonitor,infraredMonitorLog);
        LogRequestUtil.setRequest(infraredMonitorLog,request);
        System.out.println(infraredMonitorLog);
        baseMapper.insert(infraredMonitorLog);
    }

    //删除某个id的InfraredMonitorLog
    @Override
    public void deleteInfraredMonitorLogById(int id) {
        int i = baseMapper.deleteById(id);
        if(i < 1){
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    //删除某个设备id的所有日志
    @Override
    public void deleteInfraredMonitorLogByDeviceId(String deviceId) {
        int i = baseMapper.delete(new QueryWrapper<InfraredMonitorLog>().eq("device_id", deviceId));
        if(i < 1){
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    //查redis并保存
    @Override
    public void saveByDeviceId(String deviceId) {
        List<Object> objects = redisUtil.lGet("Infrared:" + deviceId, 0, -1);
        if(CollectionUtils.isEmpty(objects)){
            return;
        }
        List<InfraredMonitorLog> infraredMonitorLogs = BeanListUtils.copyListProperties(objects, InfraredMonitorLog::new);
        this.saveBatch(infraredMonitorLogs);
    }

    @Override
    public Page<InfraredMonitorLog> getInfraredListByDto(int pageNum, int limit, BaseDeviceLogDto dto) {
        Page<InfraredMonitorLog> page = new Page<>(pageNum, limit);
        QueryWrapper<InfraredMonitorLog> wrapper = new SqlUtil.Dynamic<InfraredMonitorLog>()
                .like("device_id", dto.getDeviceId())
                .ge("operation_date", dto.getStartDate())
                .le("operation_date", dto.getEndDate())
                .build();
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public void deleteInfraredMonitorLogByBatchId(ArrayList<Integer> batchId) {
        int i = baseMapper.deleteBatchIds(batchId);
        if(i < 1){
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    @Override
    public Page<InfraredMonitorLog> getAllInfraredLog(int pageNum, int limit) {
        Page<InfraredMonitorLog> page = new Page<>(pageNum,limit);
        return baseMapper.selectPage(page,null);
    }

}
